/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package strategy;

import java.util.Random;
import java.util.Vector;
import strategy.matchers.Criterion;

/**
 *
 * @author loki
 */
public class AgeMatch implements Match {
    public Vector<Single> allTheSingleGentlemen, allTheSingleLadies;
    
    AgeMatch(Vector<Single> allTheSingleGentlemen, Vector<Single> allTheSingleLadies) {
        this.allTheSingleGentlemen = allTheSingleGentlemen;
        this.allTheSingleLadies = allTheSingleLadies;
    }

    public Single match(Single subject, Vector<Single> database, StrategyOpgave.Method method, Object argument) {
        Vector<Single> rightAge = new Vector<Single>();
        
        Random r = new Random();
        int maxYounger = ((int[]) argument)[0];
        int maxOlder = ((int[]) argument)[1];
        for (Single single : database) {
            if (single.getAge() >= subject.getAge() - maxYounger
                    && single.getAge() <= subject.getAge() + maxOlder) {
                rightAge.add(single);
            }
        }
        if (rightAge.isEmpty()) {
            return null;
        } else {
            return rightAge.elementAt(r.nextInt(rightAge.size()));
        }
    }
    
    public String anybody(Single single) {
        if (single != null) {
            return single.toString();
        } else {
            return "Nobody out there :'(";
        }
    }
    
    public void runMatch(Single subject) {        
        
        //Single subject;
        
        System.out.println("We're going to find a match for:\n" + subject.getName());
        Vector<Single> eligibles;
        Random r = new Random();
        if (r.nextBoolean()) {
            subject = allTheSingleGentlemen.elementAt(r.nextInt(allTheSingleGentlemen.size()));
            eligibles = allTheSingleLadies;
        } else {
            subject = allTheSingleLadies.elementAt(r.nextInt(allTheSingleLadies.size()));
            eligibles = allTheSingleGentlemen;
        }

        //Single match;
        System.out.println("\n" + anybody(match(subject, eligibles, StrategyOpgave.Method.Blind, null)));

        //System.out.println("\nCity match:\n" + anybody(match(subject, eligibles, StrategyOpgave.Method.City, null)));
        int[] minMaxAge = {2, 2};
        //System.out.println("\nAge match (within 2 years):\n" + anybody(match(subject, eligibles, StrategyOpgave.Method.Age, minMaxAge)));
        Criterion[] criteria = {new Criterion(Criterion.Type.Smokes, false),
            new Criterion(Criterion.Type.Age, subject.getAge()),
            new Criterion(Criterion.Type.Weight, 0, subject.getWeight())};
        //System.out.println("\nSelection match (non smoker, same age, weights less):\n" + anybody(match(subject, eligibles, StrategyOpgave.Method.Selection, criteria)));
    }
}
